package com.elevator;

import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JFrame;
import javax.swing.JScrollPane;

import data.setting;

public class MainFrame extends JFrame {
	private static final long serialVersionUID = 1L;
	private manualView mManualView = null;
	private simulationSettingUI mMaSettingUI = null;
	private JScrollPane mScrollPane = null;
	private adminUI mAdminUI = new adminUI();
	private simulationUI mSimulationUI = null;
	public MainFrame()
	{
		Container container = this.getContentPane();
		container.setLayout(null);
		
		mAdminUI.setLocation(0, 0);
		mAdminUI.setSize(800, 600);
		mScrollPane = new JScrollPane();
        mScrollPane.getViewport().setBounds(0, 0, 1000, 700);
        mScrollPane.getViewport().add(mAdminUI);
        mScrollPane.setLocation(0, 0);
        mScrollPane.setSize(980, 640);
        container.add(mScrollPane);
		mAdminUI.setManualRunningBtnListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				if(mScrollPane != null)
				{
				    mScrollPane.getViewport().removeAll();
				    if(mManualView != null)
				    {
				        mManualView.stopTimer();
				    }
					
				}
				
				setting.sEvelatorCount = mAdminUI.mLiftNum;
				setting.sLayerCount = mAdminUI.mLayNum;
				
				mManualView = new manualView();
				mScrollPane.getViewport().add(mManualView);
				mManualView.mBackBtn.addActionListener(new ActionListener() {
                    
                    @Override
                    public void actionPerformed(ActionEvent arg0) {
                        if(mScrollPane != null)
                        {
                            mScrollPane.getViewport().removeAll();
                            if(mManualView != null)
                            {
                                mManualView.stopTimer();
                            }
                            mScrollPane.getViewport().add(mAdminUI);
                            mAdminUI.setVisible(true);
                        }
                        
                        
                    }
                });
				mManualView.invalidate();
				mManualView.updateUI();
				mAdminUI.setVisible(false);
			}
		});
		
		mAdminUI.setSimulationRunningBtnListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				if(mScrollPane != null)
				{
				    mScrollPane.getViewport().removeAll();
				}
				
				setting.sEvelatorCount = mAdminUI.mLiftNum;
				setting.sLayerCount = mAdminUI.mLayNum;
				
				mMaSettingUI = new simulationSettingUI();
				mMaSettingUI.mBackBtn.addActionListener(new ActionListener() {
                    
                    @Override
                    public void actionPerformed(ActionEvent arg0) {
                        if(mScrollPane != null)
                        {
                            mScrollPane.getViewport().removeAll();
                            mScrollPane.getViewport().add(mAdminUI);
                            mAdminUI.setVisible(true);
                        }
                        
                        
                    }
                });
				//run
				mMaSettingUI.mRunBtn.addActionListener(new ActionListener() {
					
					@Override
					public void actionPerformed(ActionEvent arg0) {
						
						MainFrame.this.runSimulation(mMaSettingUI);
					}
				});
				//run history
				mMaSettingUI.mRunFromResult.addActionListener(new ActionListener() {
					
					@Override
					public void actionPerformed(ActionEvent arg0) {
						if(mMaSettingUI.mHistoryRecordBox.getItemCount() > 0)
						{
							sqlitedb sqlitedb = new sqlitedb((String)mMaSettingUI.mHistoryRecordBox.getSelectedItem(),1);
							sqlitedb.getData(mMaSettingUI);
							MainFrame.this.runSimulation(mMaSettingUI);
							sqlitedb.closeDB();	
							mAdminUI.refeshView();
                                                        mMaSettingUI.resetUI();
						}

					}
				});
				//run &save
				mMaSettingUI.mRunSaveBtn.addActionListener(new ActionListener() {
					
					@Override
					public void actionPerformed(ActionEvent arg0) {
						if (mMaSettingUI.mFileNameField.getText().equals("")) {
							return ;
						}
						sqlitedb sqlitedb = new sqlitedb(mMaSettingUI.mFileNameField.getText(),1);
						sqlitedb.storeSettingDB(mMaSettingUI);
						MainFrame.this.runSimulation(mMaSettingUI);
						sqlitedb.closeDB();
						mMaSettingUI.getHistoryRecord();
						
					}
				});

				mScrollPane.getViewport().add(mMaSettingUI);
				
				mMaSettingUI.invalidate();
				mMaSettingUI.updateUI();
				mAdminUI.setVisible(false);
			}
		});
		mAdminUI.setSize(1000, 700);
		this.setSize(1000, 700);
		this.setTitle("Lift Simulation");
		this.setDefaultCloseOperation(EXIT_ON_CLOSE);
	}
	
    private void runSimulation(simulationSettingUI settingUI)
	{
		mSimulationUI =  new simulationUI(settingUI.mProbobilityBox.getSelectedIndex()+1,settingUI.mFromLayer,settingUI.mToLayer);
		if (mScrollPane != null) {
		    mScrollPane.getViewport().removeAll();
		}
		mSimulationUI.mBackBtn.addActionListener(new ActionListener() {
            
            @Override
            public void actionPerformed(ActionEvent arg0) {
                if(mScrollPane != null)
                {
                    mScrollPane.getViewport().removeAll();
                    mScrollPane.getViewport().add(mMaSettingUI);
                    if (mSimulationUI != null) {
                        mSimulationUI.stopTimer();
                    }
                    mMaSettingUI.setVisible(true);
                }
            }
        });
        
		mScrollPane.getViewport().add(mSimulationUI);
		if (settingUI.mPreTimeBox.getSelectedIndex() == 1) {
			mSimulationUI.mElevatorViews.get(0).mTimeLater= 100;
			mSimulationUI.mElevatorViews.get(0).mTimeTick = 200;
		}
		else
		{
	          mSimulationUI.mElevatorViews.get(0).mTimeLater= 1000;
	            mSimulationUI.mElevatorViews.get(0).mTimeTick = 500;
		}
		mSimulationUI.invalidate();
		mSimulationUI.updateUI();
		MainFrame.this.getContentPane().add(mScrollPane);
		mScrollPane.setSize(980, 640);
		mMaSettingUI.setVisible(false);
		mSimulationUI.mElevatorViews.get(0).startTimer();
	}
}
